package net.mihome.app.ui.view;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;


import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import net.mihome.app.AppContext;
import net.mihome.app.AppException;
import net.mihome.app.adapter.ListViewArticleAdapter;
import net.mihome.app.adapter.ListViewNewsAdapter;
import net.mihome.app.adapter.ListViewShopMenuAdapter;
import net.mihome.app.bean.Article;
import net.mihome.app.bean.News;
import net.mihome.app.bean.NewsList;
import net.mihome.app.bean.Shop;
import net.mihome.app.bean.ShopMenu;
import net.mihome.app.common.UIHelper;
import net.mihome.app.widget.PullToRefreshListView;
import net.mihome.app.R;

public class MihomeShopMenuView extends XListView {
	private Handler handler;
	private ListViewShopMenuAdapter adapter;
	private List<ShopMenu> articleData = new ArrayList<ShopMenu>();
	private AppContext appContext;
	private int begin=0;
	private int num=10;
	private int newsTypeId=Article.ARTICLE_TYPE_BANZHENG;
	private MyViewLinster myViewLinster;
	private int shopId;
	private final static int TYPE_onRefresh=101;
	private final static int TYPE_initData=102;
	private final static int TYPE_except=103;
	public MihomeShopMenuView(Context context, AttributeSet attrs) {
		super(context, attrs);;
		afterInit();
		// TODO Auto-generated constructor stub
	}
	public MihomeShopMenuView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		afterInit();
		// TODO Auto-generated constructor stub
	}
	private void afterInit(){
		adapter = new ListViewShopMenuAdapter(getContext(), articleData,R.layout.mihome_shop_menu_listitem);
		setPullLoadEnable(true);
		setPullRefreshEnable(true);
		setAdapter(adapter);
		appContext=(AppContext) getContext().getApplicationContext();
		handler=new Handler(new Handler.Callback() {
			@Override
			public boolean handleMessage(Message msg) {
				// TODO Auto-generated method stub
					adapter.notifyDataSetChanged();
					stopRefresh();
					stopLoadMore();
					setRefreshTime("不久前");
					switch (msg.what) {
					case TYPE_onRefresh:
						articleData.clear();
						articleData.addAll(0,(Collection<? extends ShopMenu>) msg.obj);
						break;
					case TYPE_initData:
						articleData.clear();
						articleData.addAll((Collection<? extends ShopMenu>) msg.obj);
						break;
					case TYPE_except:
						Toast.makeText(MihomeShopMenuView.this.getContext(),"发生异常",Toast.LENGTH_SHORT).show();
					default:
						break;
					}
					if(myViewLinster!=null){
						myViewLinster.onLoadComplete();
					}
				return false;
			}
		});
		setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				new Thread(new Runnable() {
					@Override
					public void run() {
						Message message=new Message();
						message.what=TYPE_onRefresh;
						message.obj=new ArrayList<ShopMenu>();
						try {
							begin=0;
							num=10;
							List<ShopMenu> list=appContext.getShopMenus(shopId, begin, num);
							if(list!=null&&!list.isEmpty()&&getVisibility()==View.VISIBLE){
								message.obj=list;
//								articleData.clear();
//								articleData.addAll(0,list);
							}
						} catch (AppException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							message.what=TYPE_except;
						}
						handler.sendMessage(message);
					}
				}).start();
			}
			
			@Override
			public void onLoadMore() {
				try {
					begin=(begin+1)*num;
					List<ShopMenu> list=appContext.getShopMenus(newsTypeId, begin, num);
					if(list!=null&&!list.isEmpty()&&getVisibility()==View.VISIBLE){
						articleData.addAll(list);
					}
				} catch (AppException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				handler.sendEmptyMessage(1);
			}
		});

		setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int arg2,
					long arg3) {
				Article news = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					news = (Article) view.getTag();
				} else {
					TextView tv = (TextView) view
							.findViewById(R.id.news_listitem_title);
					news = (Article) tv.getTag();
				}
				if (news == null)
					return;
				// 跳转到新闻详情
				UIHelper.showArticleDetail(view.getContext(), news.getId());
			}
		});
	}
	public void initData(int shop_id){
		this.shopId=shop_id;
		if(myViewLinster!=null){
			myViewLinster.onBeforeLoad();
		}
        startRefresh();
//		new Thread(new Runnable() {
//			@Override
//			public void run() {
//				Message message=new Message();
//				message.what=TYPE_initData;
//				message.obj=new ArrayList<ShopMenu>();
//				try {
//
//					begin=0;
//					num=10;
//					List<ShopMenu> list=appContext.getShopMenus(shopId, begin, num);
//					if(list!=null&&!list.isEmpty()&&getVisibility()==View.VISIBLE){
//						message.obj=list;
////						articleData.clear();
////						articleData.addAll(list);
//					}
//				} catch (AppException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//					message.what=TYPE_except;
//				}
//				handler.sendMessage(message);
//			}
//		}).start();
	}
	public MyViewLinster getMyViewLinster() {
		return myViewLinster;
	}
	public void setMyViewLinster(MyViewLinster myViewLinster) {
		this.myViewLinster = myViewLinster;
	}
	
}
